package com.atguigu.day03;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
                .fromElements(1,2,3,4,5,6,7,8)
                .setParallelism(1)
                .partitionCustom(
                        // 泛型：key的泛型
                        new Partitioner<Integer>() {
                            @Override
                            public int partition(Integer key, int numPartitions) {
                                if (key == 0 || key == 1) {
                                    return 0; // 将key为0或者1的数据发送到索引为0的print的并行子任务
                                } else {
                                    return 1; // 将key为2的数据发送到索引为1的print的并行子任务
                                }
                            }
                        },
                        new KeySelector<Integer, Integer>() {
                            @Override
                            public Integer getKey(Integer input) throws Exception {
                                return input % 3; // 将除以3的余数设置为输入数据的key
                            }
                        }
                )
                .print()
                .setParallelism(4);

        env.execute();
    }
}
